package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)"json:"username"`
	Password string `gorm:"type:varchar(225)"json:"password"`
}

type Userrolegxb struct {
	gorm.Model
	Uid int `gorm:"type:char(11)"json:"uid"`
	Rid int `gorm:"type:char(11)"json:"rid"`
}

type Role struct {
	gorm.Model
	Rolename string `gorm:"type:varchar(30)"json:"rolename"`
}
type Rolequanhxb struct {
	gorm.Model
	Qid int `gorm:"type:char(11)"json:"qid"`
	Rid int `gorm:"type:char(11)"json:"rid"`
}
type Quan struct {
	gorm.Model
	Quanname string `gorm:"type:varchar(30)"json:"quanname"`
}
type Comans struct {
	gorm.Model
	Comanname string `gorm:"type:varchar(30)"json:"comanname"`
}

type Types struct {
	gorm.Model
	Typename string `gorm:"type:varchar(30)"json:"typename"`
}

type Orders struct {
	gorm.Model
	Ordersname string `gorm:"type:varchar(30)"json:"ordersname"`
	Cid        int    `gorm:"type:char(11)"json:"cid"`
	Tid        int    `gorm:"type:char(11)"json:"tid"`
}

func (*User) Dengs(username string, user *User) error {
	return DB.Where("username=?", username).First(user).Error
}
func (r *Rolequanhxb) DataAdd(data *Rolequanhxb) error {
	tx := DB.Begin()
	err := DB.Debug().Create(data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (r *Role) Userrolegxblists(uid int) error {
	err := DB.Table("roles").
		Joins("LEFT JOIN userrolegxbs on roles.id=userrolegxbs.rid").
		Where(" userrolegxbs.uid=?", uid).Find(&r).Error
	return err
}
func (q *Quan) Rolequanhxbss(rid int) error {
	err := DB.Table("quans").
		Joins("LEFT JOIN rolequanhxbs on rolequanhxbs.qid=quans.id").
		Where("rolequanhxbs.rid=?", rid).
		Find(&q).Error
	return err
}
func (c *Comans) ComansAdds(data *Comans) error {
	tx := DB.Begin()
	err := DB.Debug().Create(data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (c *Orders) COrdersAdds(data *Orders) error {
	tx := DB.Begin()
	err := DB.Debug().Create(data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
